<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useBgwStores } from '@/stores/bgwStores';
import { storeToRefs } from 'pinia';
import { useTopicStores } from '@/stores/topicStores';
import { PageConstant } from '@/common/Constant';

const bgwStores = useBgwStores();
const topicStore =  useTopicStores()
const { getCategoryList } = bgwStores;

const { categoryPageList } = storeToRefs(bgwStores);
const {currentTopic,subCategories} = storeToRefs(topicStore);

const router = useRouter();
const topicList = ref<any>([]);
const loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(10);
const hasMore = ref<boolean>(true);

const handleCardClick = (item:any) => {
    currentTopic.value = item;
    subCategories.value = item.towLevelCategoryList || [];
    router.push({
        path: PageConstant.bgwDetailPage + '/' + item.id,
        query: {
            category_id: item.id
        }
    })
}

// 获取题目分类数据
const fetchTopicList = async () => {
    if (!hasMore.value || loading.value) return;

    try {
        loading.value = true;
        await getCategoryList(currentPage.value, pageSize.value);
        const newItems: any = categoryPageList.value?.records.map(item => ({
            id: item.id,
            title: item.categoryName,
            description: `${item.categoryName}相关知识点总结...`,
            icon: item.imageUrl,
            towLevelCategoryList: item.towLevelCategoryList
        }));

        topicList.value = [...topicList.value, ...newItems];

        // 判断是否还有更多数据
        hasMore.value = (categoryPageList.value?.total && currentPage.value * categoryPageList.value.size < categoryPageList.value?.total) as boolean;
        if (hasMore.value) {
            currentPage.value++;
        }
    } catch (error) {
        ElMessage.error('获取分类列表失败');
        console.error('获取分类列表失败:', error);
    } finally {
        loading.value = false;
    }
};

// 监听滚动事件
const handleScroll = () => {
    const scrollHeight = document.documentElement.scrollHeight;
    const scrollTop = document.documentElement.scrollTop;
    const clientHeight = document.documentElement.clientHeight;

    // 当距离底部100px时加载更多
    if (scrollHeight - scrollTop - clientHeight < 100) {
        fetchTopicList();
    }
};

onMounted(() => {
    fetchTopicList();
    window.addEventListener('scroll', handleScroll);
});

onUnmounted(() => {
    window.removeEventListener('scroll', handleScroll);
});
</script>

<template>
    <div class="topic-cards">
        <el-row :gutter="20">
            <el-col :span="6" v-for="item in topicList" :key="item.id">
                <el-card class="topic-card" shadow="hover" @click="handleCardClick(item)">
                    <div class="card-content">
                        <div class="icon-title">
                            <img :src="item.icon" :alt="item.title" class="topic-icon">
                            <div class="title">{{ item.title }}</div>
                        </div>
                        <div class="description">{{ item.description }}</div>
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <!-- 加载状态 -->
        <div v-if="loading" class="loading-more">
            <el-skeleton :rows="1" animated />
        </div>

        <!-- 没有更多数据 -->
        <div v-if="!hasMore && topicList.length > 0" class="no-more">
            没有更多数据了
        </div>
    </div>
</template>

<style scoped>
.topic-cards {
    padding: 20px;
}

.topic-card {
    margin-bottom: 20px;
    cursor: pointer;
    transition: all 0.3s;
}

.topic-card:hover {
    transform: translateY(-5px);
}

.card-content {
    padding: 10px 0;
}

.icon-title {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.topic-icon {
    width: 32px;
    height: 32px;
    object-fit: contain;
}

.title {
    font-size: 16px;
    font-weight: 500;
    color: #333;
}

.description {
    font-size: 14px;
    color: #666;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

:deep(.el-card) {
    border-radius: 8px;
    border: none;
}

:deep(.el-card__body) {
    padding: 16px;
}

:deep(.el-skeleton-item) {
    margin-bottom: 20px;
    border-radius: 8px;
}

.loading-more {
    padding: 20px 0;
    text-align: center;
}

.no-more {
    padding: 20px 0;
    text-align: center;
    color: white;
    font-size: 14px;
}
</style>